package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.Setmeal;
import cn.itcast.service.AddressService;
import cn.itcast.service.MemberService;
import cn.itcast.service.OrderService;
import cn.itcast.service.SetmealService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

@RestController
@RequestMapping(path = "/order")
public class OrderInfoController {

    @Reference
    private OrderService orderService;

    @Reference
    private MemberService memberService;

    @Reference
    private AddressService addressService;

    @Reference
    private SetmealService setmealService;

    /**
     * 提交订单详情
     *
     * @param map
     * @return
     * @throws ParseException
     */
    @PostMapping("/submit")
    public Result submit(@RequestBody Map<String, String> map) throws ParseException {
        //在订单项添加数据
        Order order = new Order();
        order.setOrderType("微信预约");
        order.setSetmealId(Integer.valueOf(map.get("setmealId")));
        order.setPhoneNumber(map.get("phoneNumber"));
        order.setName(map.get("name"));
        order.setIdCard(map.get("idCard"));
        order.setOrderDate(new SimpleDateFormat("yy-MM-dd").parse(map.get("orderDate")));
        order.setSex(map.get("sex"));
        order.setAddressId(Integer.valueOf(map.get("addressId")));
        Result submit = orderService.submit(order);
        return submit;
    }

    @GetMapping("/{id}")
    public Result orderId(@PathVariable(value = "id") String id) {
        Order order = orderService.findById(id);
        Integer setmealId = order.getSetmealId();
        Setmeal setmeal = setmealService.findById(setmealId);
        Integer addressId = order.getAddressId();
        Address address = addressService.findAddressById(addressId);
        order.setSetmeal(setmeal);
        order.setAddress(address);

        return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, order);
    }
}
